<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
    />
    <meta name="author" content="林镭特" />
    <title></title>

    <style>
      div {
        position: relative;
        width: 500px;
        height: 400px;
      }
      img {
        position: absolute;
        top: 0;
        left: 0;
        display: none;
      }
      .active {
        display: block;
      }
    </style>
  </head>
  <body>
    <div>
      <img src="img/1.jpg" alt="" class="active" />
      <img src="img/2.jpg" alt="" />
      <img src="img/3.jpg" alt="" />
      <img src="img/4.jpg" alt="" />
    </div>
    <script>
      var imgs = document.querySelectorAll("img");
      var index = 0;
      var lastindex = index;

      setInterval(show, 1000);

      function show() {
        imgs[lastindex].className = "";
        index++;
        if (index == imgs.length) {
          index = 0;
        }
        imgs[index].className = "active";
        lastindex = index;
      }
    </script>

  </body>
</html>
